package me

import grails.converters.JSON
import groovy.sql.Sql
import grails.plugins.rest.client.RestBuilder
import org.springframework.http.HttpStatus
import org.springframework.util.*


class UtilController {

	def dataSource


	def getOptionName={p->
		def optionName = p.key.replace("Width","宽").replace("Height","高").replace("width","宽").replace("height","高")
			.replace("padding","边距").replace("Color","颜色").replace("color","颜色").replace("font","文字").replace("Style","样式").replace("Weight","粗细")
			.replace("Size","大小").replace("shadow","阴影").replace("align","对齐方式").replace("Family","字体").replace("border","边框").replace("Border","边框")
			.replace("Align","对齐方式").replace("text","文字").replace("vertical","垂直").replace("rich","富文本").replace("Axis","坐标").replace("axis","坐标")
			.replace("label","标签").replace("Label","标签").replace("Offset","偏移").replace("Shadow","阴影").replace("background","背景").replace("Blur","模糊")
			.replace("Radius","半径").replace("radius","半径").replace("rotate","旋转").replace("icon","图标").replace("size","大小").replace("show","是否显示")
			.replace("handle","句柄").replace("type","类型").replace("throttle","周期").replace("opacity","透明度").replace("interval","间隔").replace("Pointer","间隔")
			.replace("Min","最小").replace("line","行").replace("inside","朝内").replace("margin","边缘").replace("Line","线").replace("Max","最大")
			.replace("formatter","格式化").replace("area","区域").replace("Area","区域").replace("symbol","标志").replace("onZero","是否在零刻度")
			.replace("split","分割").replace("name","名称").replace("Text","文字").replace("Index","位置").replace("With","根据").replace("length","长度")
			.replace("Tick","刻度").replace("precision","精度").replace("trigger","触发").replace("Tooltip","提示框").replace("value","数值")
			.replace("status","状态").replace("dataset","数据集").replace("source","来源").replace("Header","列头").replace("dimensions","维度")
			.replace("autoPlay","是否自动播放").replace("autoPlay","是否自动播放").replace("item","条目").replace("play","播放").replace("Icons","图标").replace("Icon","图标")
			.replace("emphasis","重点").replace("snap","自动吸附").replace("scale","比例").replace("data","数据").replace("Location","位置").replace("min","最小")
			.replace("interval","间隔").replace("Gap","间隔").replace("inverse","翻转").replace("Number","数值").replace("max","最大").replace("boundary","边线")
			.replace("grid","网格").replace("Rotate","旋转").replace("silent","静态无法交互").replace("position","位置").replace("parallel","平行")
			.replace("Interval","间隔").replace("offset","偏移").replace("logBase","对数轴的底数").replace("Select","选择").replace("Event","事件") 
			.replace("dim","维度序号").replace("realtime","实时刷新").replace("legend","图例").replace("page","页面").replace("horizontal","水平") 
			.replace("top","顶距").replace("bottom","底距").replace("left","左距").replace("right","右距").replace("geo","地理坐标")
			.replace("subtarget","副标题超链接").replace("sublink","副标题文本超链接").replace("subtext","副标题文本").replace("target","超链接")
			.replace("Button","按钮").replace("Position","位置").replace("current","目前").replace("loop","循环").replace("rewind","反向播放")
			.replace("control","控制").replace("Type","类型").replace("checkpoint","检查点").replace("animation","动画").replace("time","时间").replace("link","联动")
			.replace("indicator","指示器").replace("Next","前进").replace("feature","配置项").replace("saveAsImage","保存为图片").replace("contain","包含")
			.replace("toolbox","工具栏").replace("polar","极坐标").replace("tooltip","提示框").replace("orient","布局").replace("range","范围").replace("cell","单元格")
			.replace("Count","数量").replace("Name","名称").replace("description","描述").replace("title","标题").replace("hide","隐藏").replace("Delay","延迟")
			.replace("sub","子标题").replace("confine","限制").replace("transition","过渡").replace("Duration","时间").replace("graphic","图形").replace("single","单")
			.replace("calendar","日历").replace("aria","无障碍访问").replace("general","通用").replace("Default","默认值").replace("Zoom","缩放")
			.replace("Update","更新").replace("angle","角度").replace("Threshold","临界值").replace("brush","区域选择").replace("radar","雷达坐标")
			.replace("visualMap","视觉映射").replace("blend","混合").replace("Mode","模式").replace("use","使用")
			if (p.key=="") {
				optionName = "文字字体"
			}
			if (p.key=="") {
				optionName = "样式"
			}
		return optionName	
    }


    def getOptionDesc={p->
    	def optionDesc
    	optionDesc = p.value.description.replace("<p>","").replace("</p>","").replace("<code>","").replace("</code>","")
			.replace("<ul>","").replace("</ul>","").replace("<li>","").replace("</li>","").replace("&#39;","").replace("<blockquote>","")
			.replace("</blockquote>","").replace("<pre>","").replace("</pre>","").replace("<strong>","").replace("</strong>","")
			.replace("<br>","").replace("<hr>","").replace('<a href="#graphic.elements-image">',"").replace('</a',"")
			.replace('<a href="#angleAxis.type">',"").replace('<code class="lang-js">',"")
		return optionDesc	
	
    }


    def parseProperties = {p,children,prefix->
		def hm = [:]
		hm.id = prefix+"_"+p.key
		hm.optionId = p.key
		hm.children = []

		if(p.value==null){
			return
		}
		if (p.value.properties!=null && p.value.properties.size()!=0) {
			//println p.value.properties.size().toString()
			p.value.properties.each{it->
				parseProperties(it,hm.children,hm.id)
			}
		}

		if (p.value.items!=null && p.value.items.size()!=0) {
			def itemsChildren = []
			if (p.value.items.anyOf!=null) {
				p.value.items.anyOf.eachWithIndex{it,i->
					def thm = [:]
					thm.id = prefix+"_items_andOf_"+(i+1)
					thm.leaf = false
					thm.optionId = i+1
					thm.optionType = it.type
					thm.optionDesc = it.description.replace("<p>","").replace("</p>","")
					thm.children = []
					it.properties.each{it1->
						def thm1 = [:]
						thm1.id = hm.id+"_items_andOf_"+(i+1)+"_"+it1.key
						thm1.leaf = true
						thm1.optionId = it1.key
						thm1.optionType = it1.value['type'] 
						def optionDefault = it1.value['default'].toString()
						if (optionDefault!=null) {
							optionDefault = optionDefault.replace("'","")
						}
						thm1.optionDefault = optionDefault
						thm1.optionDesc = getOptionDesc(it1)
						thm1.optionName = getOptionName(it1)
						thm1.rawDesc = it1.value['description']
						thm1.qtip1="英文属性："+thm1.optionId+"<br>中文属性："+thm1.optionName+"<br>类型："+thm1.optionType+"<br>默认值："+thm1.optionDefault+"<br>描述："+it1.value['description']	

						thm.children.add(thm1) 
					}
					
					itemsChildren.add(thm)	
				}
	 
				hm.children = [
					[
						optionId:"items",
						id:hm.id+"_items",
						leaf:false,
						optionType:"Array",
						optionDesc:p.value.items.description?p.value.items.description:"",
						qtip1:"英文属性：items<br>中文属性：<br>类型：Array<br>默认值：<br>描述："+p.value.items.description,
						children:[
							[
								id:hm.id+"_items_anyOf",
								optionId:"anyOf",
								leaf:false,
								children:itemsChildren
							]
						]
					]
				]
			} else {
				p.value.items.properties.each{it1->
						def thm1 = [:]
						thm1.id = hm.id+"_items_"+it1.key
						thm1.leaf = true
						thm1.optionId = it1.key
						thm1.optionType = it1.value['type'] 
						def optionDefault = it1.value['default'].toString()
						if (optionDefault!=null) {
							optionDefault = optionDefault.replace("'","")
						}
						thm1.optionDefault = optionDefault
						thm1.optionDesc = getOptionDesc(it1)
						thm1.rawDesc = it1.value['description']
						thm1.optionName = getOptionName(it1)
						thm1.qtip1="英文属性："+thm1.optionId+"<br>中文属性："+thm1.optionName+"<br>类型："+thm1.optionType+"<br>默认值："+thm1.optionDefault+"<br>描述："+it1.value['description']	

						itemsChildren.add(thm1) 
				}		
				hm.children = [
					[
						id:hm.id+"_items",
						optionId:"items",
						leaf:false,
						optionType:"Array",
						qtip1:"英文属性：items<br>中文属性：<br>类型：Array<br>默认值：<br>描述："+p.value.items.description,	
						optionDesc:p.value.items.description?p.value.items.description:"",
						children:itemsChildren
					]
				]
			}
		
		}

		if (hm.children.size()==0) {
			hm.leaf = true
		} else {
			hm.leaf = false
		}
		hm.expanded = true

		if (p.value.type!=null) {
		 	hm.optionType=p.value.type.toString()
		}
		

		if (p.value.description!=null) {
			hm.optionDesc = getOptionDesc(p)
			
		} else {
			hm.optionDesc = ""
		}

		hm.optionName = getOptionName(p)
		if (p.value.default!=null) {
			def optionDefault = p.value['default'].toString()
			if (optionDefault!=null) {
				optionDefault = optionDefault.replace("'","").replace("\"","")
			}
			hm.optionDefault = optionDefault
			//hm.optionDefault = p.value.default
		}

		hm.rawDesc = p.value['description']
		hm.qtip1="英文属性："+hm.optionId+"<br>中文属性："+hm.optionName+"<br>类型："+hm.optionType+"<br>默认值："+hm.optionDefault+"<br>描述："+p.value.description
		children.add(hm)
	}


    def echartsapi() { 
    	def sql = new Sql(dataSource)
    	sql.execute("delete from base_attrs_info_copy")
    	def results =[:]
    	def rest = new RestBuilder()
		def resp = rest.get("http://www.echartsjs.com/documents/cn/option.json?_v_=1536949947336")
		resp.json.option.properties.each{
			println it.key
		 	parseProperties(results,it,"")
		}
		


		render results  as JSON

    }

    

}
